import { defineStore } from 'pinia'

interface UserInfoProp {
  avatar?: string
  name: string
  token: string
}

const useAuth = defineStore({
  id: 'auth',
  state: () => ({
    userInfo: {
      avatar: '',
      name: '',
      token: ''
    },
    store: [],
    storeId: ''
  }),
  persist: {
    enabled: true, // 启用
    strategies: [
      // storage 可选localStorage或sessionStorage
      // paths 给指定数据持久化
      { key: 'auth', storage: localStorage, paths: ['userInfo', 'store', 'storeId'] }
    ]
  },
  actions: {
    setUserInfo(userInfo: UserInfoProp) {
      this.userInfo = userInfo
    },
    setStore(store: []) {
      this.store = store
    },
    setStoreId(storeId: '') {
      this.storeId = storeId
    }
  }
})
export { useAuth }
